package com.flyoil.petromp.mvp_m.CourseListModel;

import com.flyoil.petromp.Constant.ConstantNetworkCode;
import com.flyoil.petromp.apiservice.OnObserverRetrofitResetCallBack;
import com.flyoil.petromp.base.BaseEntity;
import com.flyoil.petromp.entity.entity_course_list.CourseCertainEntity;
import com.flyoil.petromp.base.BaseModel;
import com.flyoil.petromp.entity.entity_course_list.GoodsOrderListEntity;
import com.flyoil.petromp.mvp_p.Listener.OnPresenterListener;
import com.flyoil.petromp.utils.OrderUtl.OrderListUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by songzhiyin on 2017/10/31.
 * 物资订单列表数据处理类
 */

public class GoodsOrderListModel implements BaseModel {

    @Override
    public void loadNetworkData(Map<String, Object> map, final OnPresenterListener listener) {
        httpModel.getCourseGoodsOrderList(map, new OnObserverRetrofitResetCallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity model) {
                super.onSuccess(model);
                if(ConstantNetworkCode.SuccedCode!=model.getCode()){
                    listener.onsucced(null);
                }
                GoodsOrderListEntity goodsOrderListEntity = null;
                if (model instanceof GoodsOrderListEntity) {
                    goodsOrderListEntity = (GoodsOrderListEntity) model;
                }
                if (goodsOrderListEntity != null && goodsOrderListEntity.getData() != null && goodsOrderListEntity.getData().getList() != null) {
                    List<CourseCertainEntity> courseCertainEntities = new ArrayList<>();
                    for (int i = 0; i < goodsOrderListEntity.getData().getList().size(); i++) {
                        GoodsOrderListEntity.DataBean.ListBean data = goodsOrderListEntity.getData().getList().get(i);
                        CourseCertainEntity item = new CourseCertainEntity();
                        item.setId(data.getId());
                        item.setTitle(data.getDemandName());
                        item.setTime(data.getOrderDateLabel());
                        item.setNumber(data.getSn());
                        item.setType(OrderListUtil.GOODS_ORDER);
                        courseCertainEntities.add(item);
                    }
                    listener.onsucced(courseCertainEntities);
                } else {
                    listener.onsucced(null);
                }
            }

            @Override
            public void onFailure(String msg) {
                listener.onsucced(null);
            }
        });
    }
}
